package com.meituan.catering.management.order.remote.model.converter;

import com.meituan.catering.management.order.remote.model.response.ProductDetailRemoteResponse;
import com.meituan.catering.management.product.api.thrift.model.response.ProductDetailThriftResponse;

import java.math.BigDecimal;
import java.util.stream.Collectors;

/**
 * 从其他数据模型向订单ThriftResponse的转换器
 */
public abstract class ProductRemoteConverter {
    /**
     * 根据BO转为 thriftVO
     * @return
     */
    public static ProductDetailRemoteResponse fromBO(ProductDetailThriftResponse productThrift) {

        if (productThrift == null) {
            return null;
        } else {
            ProductDetailRemoteResponse response = new ProductDetailRemoteResponse();
            response.setId(productThrift.getId());
            response.setTenantId(productThrift.getTenantId());
            response.setVersion(productThrift.getVersion());
            response.getAuditing().setCreatedBy(productThrift.getAuditing().getCreatedBy());
            /**'
             *  时间转Long
             */
            response.getAuditing().setCreatedAtTimestamp(productThrift.getAuditing().getCreatedAtTimestamp());
            response.getAuditing().setCreatedBy(productThrift.getAuditing().getCreatedBy());
            response.getAuditing().setLastModifiedBy(productThrift.getAuditing().getLastModifiedBy());
            response.getAuditing().setLastModifiedAtTimestamp(productThrift.getAuditing().getLastModifiedAtTimestamp());

            response.setName(productThrift.getName());

            response.setUnitPrice(BigDecimal.valueOf(productThrift.getUnitPrice()));
            response.setUnitOfMeasure(productThrift.getUnitOfMeasure());
            response.setMinSalesQuantity(BigDecimal.valueOf(productThrift.getMinSalesQuantity()));
            response.setIncreaseSalesQuantity(BigDecimal.valueOf(productThrift.getIncreaseSalesQuantity()));
            response.setDescription(productThrift.getDescription());
            response.setEnabled(productThrift.getEnabled());

            response.setVersion(productThrift.getVersion());

            /**
             * 转化 加料组 和方法组
             */
            response.getAccessoryGroups().addAll(productThrift.getAccessoryGroups().stream().map(ProductRemoteConverter::fromVOToAcc).collect(Collectors.toList()));
            response.getMethodGroups().addAll(productThrift.getMethodGroups().stream().map(ProductRemoteConverter::fromVOToMeth).collect(Collectors.toList()));

            return response;
        }
    }
    /**
     * 转化 加料
     * @param accessoryGroupBO
     * @return
     */
    public static ProductDetailRemoteResponse.AccessoryGroup fromVOToAcc(ProductDetailThriftResponse.AccessoryGroup accessoryGroupBO){
        ProductDetailRemoteResponse.AccessoryGroup accessoryGroupVO=new ProductDetailRemoteResponse.AccessoryGroup();
        accessoryGroupVO.setName(accessoryGroupBO.getName());
        accessoryGroupVO.getOptions().addAll(accessoryGroupBO.getOptions().stream().map(ProductRemoteConverter::fromVOToAccOption).collect(Collectors.toList()));
        return accessoryGroupVO;
    }
    public static ProductDetailRemoteResponse.AccessoryGroup.Option fromVOToAccOption(ProductDetailThriftResponse.AccessoryGroup.Option optionBO){
        ProductDetailRemoteResponse.AccessoryGroup.Option optionVO=new ProductDetailRemoteResponse.AccessoryGroup.Option();
        optionVO.setId(optionBO.getId());
        optionVO.setName(optionBO.getName());
        optionVO.setUnitPrice(BigDecimal.valueOf(optionBO.getUnitPrice()));
        optionVO.setUnitOfMeasure(optionBO.getUnitOfMeasure());
        return optionVO;
    }


    /**
     * 转换做法组
     * @param methodGroupBO
     * @return
     */

    public static ProductDetailRemoteResponse.MethodGroup fromVOToMeth( ProductDetailThriftResponse.MethodGroup methodGroupBO){
        ProductDetailRemoteResponse.MethodGroup methodGroupVO=new ProductDetailRemoteResponse.MethodGroup();

        methodGroupVO.setName(methodGroupBO.getName());
        methodGroupVO.getOptions().addAll(methodGroupBO.getOptions().stream().map(ProductRemoteConverter::fromVOToMethOption).collect(Collectors.toList()));
        return methodGroupVO;
    }

    public static ProductDetailRemoteResponse.MethodGroup.Option fromVOToMethOption(ProductDetailThriftResponse.MethodGroup.Option optionBO){
        ProductDetailRemoteResponse.MethodGroup.Option optionVO=new ProductDetailRemoteResponse.MethodGroup.Option();
        optionVO.setId(optionBO.getId());
        optionVO.setName(optionBO.getName());
        return optionVO;
    }




}
